package com.xdkj.web.realm;

import com.xdkj.model.dao.TUserDao;
import com.xdkj.model.dao.impl.TUserDaoImpl;
import com.xdkj.model.entity.TUser;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ClassName MyRealm
 * Description:
 *
 * @Author:一尘
 * @Version:1.0
 * @Date:2021-11-02-11:23
 */
public class MyRealm extends AuthorizingRealm {
    private TUserDao tUserDao = new TUserDaoImpl();
    private static final Logger log = LoggerFactory.getLogger(MyRealm.class);

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException {
        log.info("开始认证");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        System.out.println(token.getUsername());
        System.out.println(token.getPassword());
        // 查询的用户对象
        TUser tUser = tUserDao.queryByName(token.getUsername());
        // 进行密码加密
        ByteSource source = ByteSource.Util.bytes("xdkj");
        // 密码加密
        SimpleHash hashedPasswordBase64 = new Sha256Hash(token.getPassword(), source, 1024);
        System.out.println(hashedPasswordBase64);
        System.out.println(tUser.getPassword());

        return new SimpleAuthenticationInfo(tUser, tUser.getPassword(), source, this.getName());
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

}
